import apiUrl from '@/api/apiUrl'
import {Toast, MessageBox} from 'mint-ui'

export default {
    setData () {
        let params = {
            params: {
                user_id: this.$route.query.user_id
            }
        }
        this.$http.get(apiUrl.admin.owner, params).then(res => {
            if (res.data.code !== 0) {
                Toast(res.data.msg)
                this.$router.go(-1)
            } else {
                this.$set(this, 'user', res.data.result)
            }
        }).catch(err => {
            console.log(err)
        })
    },
    pass () {
        let params = {
            params: {
                user_id: this.user.id
            }
        }
        this.$set(this, 'passLoading', true)
        this.$http.get(apiUrl.admin.pass, params).then(res => {
            if (res.data.code !== 0) {
                Toast('操作失败')
            } else {
                let user = this.user
                user.status = 1
                this.$set(this, 'user', user)
            }
            this.$set(this, 'passLoading', false)
        }).catch(err => {
            console.log(err)
            this.$set(this, 'passLoading', false)
        })
    },
    password () {
        this.passwordLoading = true
        MessageBox.prompt('请输入密码').then(({ value, action }) => {
            if (value === null || value.trim().length === 0) {
                this.passwordLoading = false
                return Toast('密码不能为空')
            }

            let params = new URLSearchParams()
            params.append('password', value)
            params.append('user_id', this.user.id)
            this.$http.post(apiUrl.room.password, params).then(res => {
                if (res.data.code !== 0) {
                    Toast('操作失败')
                } else {
                    Toast('操作成功')
                }
                this.passwordLoading = false
            }).catch(err => {
                console.log(err)
            })
        }).catch(err => {
            console.log(err)
            this.passwordLoading = false
        });
    },
    createRoom () {
        this.$router.push({name: 'admin-room-create', query: {user_name: this.user.user_name}})
    }
}